<?php

/**
 * 考场
 * @author yanghao <yh38615890@sina.cn>
 * @date 07-04-12
 * @copyright (c) gandianli
 */

namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use think\exception\HttpException;
use com\gdl\newagk\dataobject\basicuser\BasicuserRequestDTO;
use com\gdl\newagk\dataobject\basicuser\BasicuserDTO;
use com\gdl\newagk\interfaces\BasicuserServiceClient;

class BasicUser extends Base
{
	//查询考场用户
    public function basicList() {
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid/d') : 0;

		$filename['title'] = $this->request->param('title') ? getStr($this->request->param('title'), 'string') : '';
		$filename['sex'] = $this->request->param('sex') ? getStr($this->request->param('sex'), 'string') : '';
		$filename['age'] = $this->request->param('age') ? $this->request->param('age') : 0;
		$filename['companyId'] = $this->request->param('companyId') ? $this->request->param('companyId/d') : 0;
		$filename['departmentId'] = $this->request->param('departmentId') ? $this->request->param('departmentId/d') : 0;
		$filename['departmentgroupId'] = $this->request->param('tem') ? $this->request->param('tem/d') : 0;
		$filename['groupId'] = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;
		$filename['vface'] = $this->request->param('vface') ? $this->request->param('vface/d') : 99;
		$filename['specialtyId'] = $this->request->param('speId') ? $this->request->param('speId/d') : 0;
		
		
		$filename['page'] = $this->request->param('p')>1 ? $this->request->param('p/d') : 1;
		$filename['pagesize'] = $this->request->param('num')>0 ? $this->request->param('num/d') : 10;
		if(!$filename['basicid']){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
         
                $result = $Basicuser->basicUserList($basic);

                if ($result->success == TRUE) {
                    if ($result->code == 1) {
						$plaform = ['msg'=>'查询成功','code'=>'200'];
                        $plaform['data'] = $result->data;
						$message = json_decode($result->message,true);
						$plaform['specialty'] = $message['Specialty'];
						$plaform['depart'] = $message['depart'];
						$plaform['company'] = $message['company'];
						$plaform['team'] = $message['team'];
						$plaform['positions'] = $message['positions'];
						$plaform['totalNumber'] = $message['num'];
                    } else {
						$plaform = ['msg'=>'查询失败','code'=>'400'];
                        $plaform['data'] = [];
						$plaform['totalNumber'] = 0;
                    }
                } else {
					$plaform = ['msg'=>'没有数据','code'=>'400'];
                    $plaform['data'] = [];
					$plaform['totalNumber'] = 0;
                    
                }
            } catch (Exception $ex) {
				$plaform = ['msg'=>'发生异常，查询失败','code'=>'400','data'=>[],'totalNumber'=>0];
            }
			$plaform['basicid'] = $filename['basicid'];
            return json_encode($plaform);
	}
	
	
	//导出考场用户
    public function basicDown() {
		ob_end_clean();
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid/d') : 0;

		$filename['title'] = $this->request->param('title') ? getStr($this->request->param('title'), 'string') : '';
		$filename['sex'] = $this->request->param('sex') ? getStr($this->request->param('sex'), 'string') : '';
		$filename['age'] = $this->request->param('age') ? $this->request->param('age') : 0;
		$filename['companyId'] = $this->request->param('companyId') ? $this->request->param('companyId/d') : 0;
		$filename['departmentId'] = $this->request->param('departmentId') ? $this->request->param('departmentId/d') : 0;
		$filename['departmentgroupId'] = $this->request->param('tem') ? $this->request->param('tem/d') : 0;
		$filename['groupId'] = $this->request->param('groupId') ? $this->request->param('groupId/d') : 0;
		$filename['vface'] = $this->request->param('vface') ? $this->request->param('vface/d') : 99;
		$filename['specialtyId'] = $this->request->param('speId') ? $this->request->param('speId/d') : 0;
		
		
		$filename['page'] = $this->request->param('p')>1 ? $this->request->param('p/d') : 1;
		$filename['pagesize'] = $this->request->param('num')>0 ? $this->request->param('num/d') : 10;
		$filename['down'] = 'excels';
		$filename['id'] = $this->request->param('id') ? $this->request->param('id') : '';
		
		
		if(!$filename['basicid']){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
         
                $result = $Basicuser->basicUserList($basic);

                if ($result->success == TRUE) {
                    if ($result->code == 1) {
                        $index = $result->data;
                    } else {
						$index = [];
                    }
                } else {
					$index = [];
                    
                }
            } catch (Exception $ex) {
				$index = [];
            }

			$header = array('ID','姓名','身份证号','绑定手机','性别','年龄','公司','部门','班组','职位','专业','照片状态');
			
			$filename = '考场人员列表'.date('YmdHis');  

		if(!empty($index)){
			if($error = \Excel::exportDistance($header, $index, $filename, '2007',['L'=>['w'=>20,'type'=>'img'],'C'=>['w'=>20,'type'=>'txt'],'D'=>['w'=>20,'type'=>'txt']])) {
				throw new Exception($error);
			}
		} else {
			echo " 没有数据~~~
			<script>
				function closeWindow(){ 
					window.opener=null;
					window.close();
				}
				window.setTimeout('closeWindow()',1000);
			</script>";
			
		}
	}
	
	//移除考场用户
    public function basicRemove() {
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid/d') : 0;
		$identity_id = $this->request->param('identity_id') ? explode(',',$this->request->param('identity_id')) : 0;

		if(!$filename['basicid'] || !$identity_id){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		$basicUserId = [0];
		foreach($identity_id as $v){
			$basicUserId[] = intval($v);
		}
		$filename['identity_id'] = implode(',',$basicUserId);
		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
                $result = $Basicuser->basicUserDel($basic);

                if ($result->success == TRUE) {
					$plaform = ['msg'=>'操作成功','code'=>'200','data'=>[]];
                } else {
					$plaform = ['msg'=>'操作失败','code'=>'400','data'=>[]];
                }
            } catch (Exception $ex) {
				$data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
            }
           
            return json_encode($plaform);
		
	}
	
	
	//添加考场用户
    public function userAdd(){
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid/d') : 0;
		$filename['identity_id'] = $this->request->param('identity_id') ? $this->request->param('identity_id') : 0;
		
		if(!$filename['basicid'] || !$filename['identity_id']){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}

		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
                $result = $Basicuser->basicUserAdd($basic);

                if ($result->success == TRUE) {
					$plaform = ['msg'=>'操作成功','code'=>'200','data'=>[]];
                } else {
					$plaform = ['msg'=>'操作失败','code'=>'400','data'=>[]];
                }
            } catch (Exception $ex) {
				$data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
            }
           
            return json_encode($plaform);
		
	}
	
	//获取考场用户
    public function userList(){
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid/d') : 0;

		
		if(!$filename['basicid']){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}

		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
                $result = $Basicuser->userList($basic);

                if ($result->success == TRUE) {
					$plaform = ['msg'=>'操作成功','code'=>'200','data'=>$result->data[0]];
                } else {
					$plaform = ['msg'=>'操作失败','code'=>'400','data'=>[]];
                }
            } catch (Exception $ex) {
				$data = ['msg'=>'发生异常，操作失败','code'=>'400','data'=>[]];
            }
           
            return json_encode($plaform);
		
	}
	
	
	//获取考场用户
    public function basicHis(){
		$filename['reg'] = $this->request->param('reg') ? $this->request->param('reg') : '';
		$filename['basicid'] = $this->request->param('basicid') ? $this->request->param('basicid') : 0;
		$filename['status'] = $this->request->param('status') ? $this->request->param('status') : 0;
		$filename['pass'] = $this->request->param('pass') ? $this->request->param('pass') : '';//failed不合格 //meets 合格
		$filename['startScore'] = $this->request->param('startScore') ? sprintf("%.2f",$this->request->param('startScore')) : 0;
		$filename['endScore'] = $this->request->param('endScore') ? sprintf("%.2f",$this->request->param('endScore')) : 0;
		$filename['page'] = $this->request->param('page')>1 ? $this->request->param('page/d') : 1;
		$filename['pagesize'] = $this->request->param('pagesize')>0 ? $this->request->param('pagesize/d') : 10;
		if(!$filename['reg'] || !$filename['basicid']){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}
		 try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
                $Basicuser = new BasicuserServiceClient(NULL);
                \ApiClient::build($Basicuser);
				$basic = new BasicuserRequestDTO();
				$basic->filename = $filename;
                $result = $Basicuser->basicHis($basic);

                if ($result->success == TRUE) {
                    if ($result->code == 1) {
						$plaform = ['msg'=>'查询成功','code'=>'200'];
                        $plaform['data'] = $result->data;
						$plaform['totalNumber'] = $result->message;
                    } else {
						$plaform = ['msg'=>'查询失败','code'=>'400'];
                        $plaform['data'] = [];
						$plaform['totalNumber'] = 0;
                    }
                } else {
					$plaform = ['msg'=>'没有数据','code'=>'400'];
                    $plaform['data'] = [];
					$plaform['totalNumber'] = 0;
                    
                }
            } catch (Exception $ex) {
				$data = ['msg'=>'发生异常，查询失败','code'=>'400','data'=>[],'totalNumber'=>0];
            }
           
            return json_encode($plaform);
		
		/*********************/
		
		$data = [
					['id'=>'1','basicName'=>'2018年9月重庆览辉科技安管控项目工程考试','truename'=>'张三','idcard'=>'500231******0022','phone'=>'13854754121','status'=>'正常','score'=>'90','suffice'=>'合格','identity_id'=>'1'],
					['id'=>'2','basicName'=>'2018年9月重庆览辉科技安管控项目工程考试','truename'=>'李四','idcard'=>'500231******0056','phone'=>'13854754121','status'=>'作弊','score'=>'--','suffice'=>'不合格','identity_id'=>'4'],
					['id'=>'3','basicName'=>'2018年9月重庆览辉科技安管控项目工程考试','truename'=>'王五','idcard'=>'500231******002X','phone'=>'13854754121','status'=>'正常','score'=>'89','suffice'=>'不合格','identity_id'=>'2'],
					['id'=>'4','basicName'=>'2018年10月山西变电2种人考试','truename'=>'张三','idcard'=>'500231******0022','phone'=>'13854754121','status'=>'正常','score'=>'50','suffice'=>'不合格','identity_id'=>'1'],
					['id'=>'5','basicName'=>'2018年6月变电内部维修模拟考试','truename'=>'王五','idcard'=>'500231******002X','phone'=>'13854754121','status'=>'正常','score'=>'95','suffice'=>'合格','identity_id'=>'2'],
				];
		$plaform = ['msg'=>'操作成功','code'=>'200','data'=>$data,'totalNumber'=>5];
		return json_encode($plaform);
	}

}
